package com.why.amallrest.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.why.common.utils.PageUtils;
import com.why.common.utils.Result;
import com.why.entity.ProductCommentsEntity;
import com.why.service.ProductCommentsService;
import com.why.vo.CommentCount;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Map;


/**
 * 商品评价
 *
 * @author why
 * @email 1585049082@qq.com
 * @date 2021-10-24 20:16:08
 */
@RestController
@RequestMapping("mall/product-comments")
public class ProductCommentsController {
    @Autowired
    private ProductCommentsService productCommentsService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    public Result list(@RequestParam Map<String, Object> params) {
        if (params.get("productId") == null) {
            return Result.error();
        }

        IPage page = productCommentsService.queryPage(params);

        return Result.ok().put("page", page);
    }


    /**
     * 查询商品评价数量
     */
    @RequestMapping("/detail-commontscount/{productId}")

    public Result info(@PathVariable("productId") String productId) {


        //查询总数量
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("product_id", productId);




        return Result.ok().put("productComments", this.productCommentsService.queryCount(Long.valueOf(productId)));
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public Result save(@RequestBody ProductCommentsEntity productComments) {
        productCommentsService.save(productComments);

        return Result.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public Result update(@RequestBody ProductCommentsEntity productComments) {
        productCommentsService.updateById(productComments);

        return Result.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")

    public Result delete(@RequestBody String[] commIds) {
        productCommentsService.removeByIds(Arrays.asList(commIds));

        return Result.ok();
    }

}
